package com.codebo.homiematch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codebo.homiematch.mapper.PostCommentMapper;
import com.codebo.homiematch.mapper.PostMapper;
import com.codebo.homiematch.mapper.UserMapper;
import com.codebo.homiematch.model.dto.PostCommentRequest;
import com.codebo.homiematch.model.entity.Post;
import com.codebo.homiematch.model.entity.PostComment;
import com.codebo.homiematch.model.entity.User;
import com.codebo.homiematch.model.vo.PostCommentVO;
import com.codebo.homiematch.service.PostCommentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 25912
 * @description 针对表【post_comment(帖子评论)】的数据库操作Service实现
 * @createDate 2025-07-23 17:46:52
 */
@Service
public class PostCommentServiceImpl extends ServiceImpl<PostCommentMapper, PostComment>
        implements PostCommentService
{
    @Resource
    private UserMapper userMapper;
    @Resource
    private PostCommentMapper postCommentMapper;

    @Override
    public List<PostCommentVO> getPostCommentList(Long id)
    {
        QueryWrapper commentQueryWrapper = new QueryWrapper();
        commentQueryWrapper.eq("post_id", id);
        List<PostComment> postCommentList = this.list(commentQueryWrapper);

        List<PostCommentVO> postCommentVOList = new ArrayList<>();


        for (PostComment postComment : postCommentList)
        {
            QueryWrapper queryWrapper = new QueryWrapper();
            Long userId = postComment.getUserId();
            queryWrapper.eq("id", userId);
            User user = userMapper.selectOne(queryWrapper);
            postCommentVOList.add(new PostCommentVO(user.getNickname(), user.getAvatar(), postComment.getText()));
        }

        return postCommentVOList;
    }

    @Override
    public void comment(PostCommentRequest postCommentRequest)
    {
        PostComment postComment = new PostComment();
        BeanUtil.copyProperties(postCommentRequest, postComment);
        postCommentMapper.insert(postComment);
    }
}




